package com.approval.approvalapp.index.cygn.qjshenqing;

import android.app.ProgressDialog;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.Day;
import com.approval.approvalapp.bean.MoniterBean;
import com.approval.approvalapp.bean.QjSqBean;
import com.approval.approvalapp.bean.UpSuccessBean;
import com.approval.approvalapp.index.cygn.qjshenqing.adapter.PhotoRvAdapter;
import com.approval.approvalapp.myself.activity.WdqjActivity;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.base.ContantActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.net.interfacepac.NetUpLoadResponse;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//请假申请
public class QjShenqingActivity extends BaseActivity {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.et)
    EditText et;
    @Bind(R.id.qjfl_tv)
    TextView qjflTv;
    @Bind(R.id.qjfl_ll)
    LinearLayout qjflLl;
    @Bind(R.id.qjlx_tv)
    TextView qjlxTv;
    @Bind(R.id.qjlx_ll)
    LinearLayout qjlxLl;
    @Bind(R.id.kssj_tv)
    TextView kssjTv;
    @Bind(R.id.kssj_ll)
    LinearLayout kssjLl;
    @Bind(R.id.jssj_tv)
    TextView jssjTv;
    @Bind(R.id.jssj_ll)
    LinearLayout jssjLl;
    @Bind(R.id.qjnum_tv)
    TextView qjnumTv;
    @Bind(R.id.commit_ll)
    LinearLayout commitLl;
    @Bind(R.id.commit_tv)
    TextView commit_tv;
    @Bind(R.id.gclx_tv)
    TextView gclxTv;
    @Bind(R.id.gclx_ll)
    LinearLayout gclxLl;
    @Bind(R.id.sc_tv)
    TextView sc_tv;
    @Bind(R.id.wjm_tv)
    TextView wjm_tv;
    @Bind(R.id.hytzr_tv)
    TextView hytzrTv;
    @Bind(R.id.hytzr_ll)
    LinearLayout hytzrLl;
    @Bind(R.id.hydd_tv)
    TextView hyddTv;
    @Bind(R.id.hydd_ll)
    LinearLayout hyddLl;
    @Bind(R.id.bthnr_tv)
    TextView bthnrTv;
    @Bind(R.id.bthnr_ll)
    LinearLayout bthnrLl;
    @Bind(R.id.hdwkhxx_ll)
    LinearLayout hdwkhxxLl;
    @Bind(R.id.djhbldhdjgzcs_tv)
    TextView djhbldhdjgzcsTv;
    @Bind(R.id.djhbldhdjgzcs_ll)
    LinearLayout djhbldhdjgzcsLl;
    @Bind(R.id.hdwhbdjgz_ll)
    LinearLayout hdwhbdjgzLl;
    @Bind(R.id.pxhyzzdw_tv)
    TextView pxhyzzdwTv;
    @Bind(R.id.pxhyzzdw_ll)
    LinearLayout pxhyzzdwLl;
    @Bind(R.id.pxhynr_tv)
    TextView pxhynrTv;
    @Bind(R.id.pxhynr_ll)
    LinearLayout pxhynrLl;
    @Bind(R.id.sc_pxhytz_tv)
    TextView scPxhytzTv;
    @Bind(R.id.wjm_pxhytz_tv)
    TextView wjmPxhytzTv;
    @Bind(R.id.pxhytz_ll)
    LinearLayout pxhytzLl;
    @Bind(R.id.wcpxhkh_ll)
    LinearLayout wcpxhkhLl;
    @Bind(R.id.sqsxmc_tv)
    TextView sqsxmcTv;
    @Bind(R.id.sqsxmc_ll)
    LinearLayout sqsxmcLl;
    @Bind(R.id.tshjmc_tv)
    TextView tshjmcTv;
    @Bind(R.id.tshjmc_ll)
    LinearLayout tshjmcLl;
    @Bind(R.id.sstshjdz_tv)
    TextView sstshjdzTv;
    @Bind(R.id.sstshjdz_ll)
    LinearLayout sstshjdzLl;
    @Bind(R.id.sstshj_ll)
    LinearLayout sstshjLl;
    @Bind(R.id.dd_tv)
    TextView ddTv;
    @Bind(R.id.dd_ll)
    LinearLayout ddLl;
    @Bind(R.id.shr_ll)
    LinearLayout shr_ll;
    @Bind(R.id.shr_tv)
    TextView shr_tv;
    private ProgressDialog progressDialog;
    private QjSqBean qjSqBean;
    private List<QjSqBean.LeaveTypeBean> leave_type;
    private List<QjSqBean.TypeBean> type;
    private List<MoniterBean> qjflList = new ArrayList<>();
    private List<MoniterBean> qjlxList = new ArrayList<>();
    private List<MoniterBean> gclxList = new ArrayList<>();
    private ArrayList<String> photos = new ArrayList<>();
    ArrayList<String> localUrl = new ArrayList<>();
    private PhotoRvAdapter adapter;
    private String kstimestr;
    private String jstimestr;
    private long kstime;
    private long jstime;
    private List<QjSqBean.GcListBean> gc_list;
    String qjlxid;
    String gclxId;
    String qjflId;


    @Override
    public int getLayoutId() {
        return R.layout.activity_qj_shenqing;
    }

    @Override
    public void initView() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在请求");
        progressDialog.setCancelable(false);
        progressDialog.show();
        JSONObject jsonObject = new JSONObject();
        if (SpUtil.getString(this,SpUtil.Usertype,"").equals("1")){
            jsonObject.put("type", "1");//中心
        }else {
            jsonObject.put("type", "0");//窗口
        }
        NetRequsetUtil.getInstance().netRequestPostJson("v1/Center/leave_add",jsonObject.toJSONString() ,new NetDownResponse() {
            @Override
            public void success(String str) {
                Log.d("QjShenqingActivitys", str);

                qjSqBean = JSON.parseObject(str, QjSqBean.class);
                leave_type = qjSqBean.getLeave_type();
                type = qjSqBean.getType();
                gc_list = qjSqBean.getGc_list();
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {
                progressDialog.dismiss();

            }
        });

        if (SpUtil.getString(this,SpUtil.Usertype,"" ). equals("1")){
            shr_ll.setVisibility(View.VISIBLE);
            shr_tv.setText(SpUtil.getString(this,SpUtil.SJLDN,"" ));
        }
    }

    @Override
    public void initData() {

    }

    String hytzrStr;
    String hyddStr;
    String bthnrStr;
    String djhbldhdjgzcsrStr;
    String pxhyzzdwStr;
    String pxhynrStr;
    String pxhytzStr;
    String sqsxmcStr;
    String tshjmcStr;
    String sstshjDzStr;
    String zzclStr;
    String ddStr;

    @OnClick({R.id.qjfl_ll, R.id.qjlx_ll, R.id.kssj_ll, R.id.jssj_ll, R.id.commit_tv, R.id.back_iv,
            R.id.gclx_ll, R.id.sc_tv,
            R.id.hytzr_ll, R.id.hydd_ll, R.id.bthnr_ll,
            R.id.djhbldhdjgzcs_ll, R.id.pxhyzzdw_ll, R.id.pxhynr_ll,
            R.id.sc_pxhytz_tv, R.id.sqsxmc_ll, R.id.tshjmc_ll, R.id.sstshjdz_ll, R.id.dd_ll})
    public void onViewClicked(View view) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        switch (view.getId()) {
            case R.id.dd_ll:
                ContantActivity.goInto(this, 14, "地点", ddStr);

                break;
            case R.id.hytzr_ll:
                ContantActivity.goInto(this, 5, "会议通知人", hytzrStr);
                break;
            case R.id.hydd_ll:
                ContantActivity.goInto(this, 6, "会议地点", hyddStr);
                break;
            case R.id.bthnr_ll:
                ContantActivity.goInto(this, 7, "标题或内容", bthnrStr);
                break;
            case R.id.djhbldhdjgzcs_ll:
                ContantActivity.goInto(this, 8, "对接汇报领导或对接工作处室(人)", djhbldhdjgzcsrStr);
                break;
            case R.id.pxhyzzdw_ll:
                ContantActivity.goInto(this, 9, "培训/会议组织单位", pxhyzzdwStr);
                break;
            case R.id.pxhynr_ll:
                ContantActivity.goInto(this, 10, "培训/会议内容", pxhynrStr);
                break;
            case R.id.sc_pxhytz_tv:
                intent.setType("*/*");//无类型限制
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, 99);
                break;
            case R.id.sqsxmc_ll:
                ContantActivity.goInto(this, 11, "申请事项名称", sqsxmcStr);
                break;
            case R.id.tshjmc_ll:
                ContantActivity.goInto(this, 12, "特殊环节名称", tshjmcStr);
                break;
            case R.id.sstshjdz_ll:
                ContantActivity.goInto(this, 13, "实施特殊环节地址", sstshjDzStr);
                break;
            case R.id.sc_tv:
                intent.setType("*/*");//无类型限制
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, 100);
                break;
            case R.id.qjfl_ll:
                qjflList.clear();
                if (type == null || type.size() == 0) {
                    return;
                }
                for (int i = 0; i < type.size(); i++) {
                    qjflList.add(new MoniterBean(type.get(i).getType(), type.get(i).getTitle()));
                }
                OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        qjflId = qjflList.get(options1).getId();
                        String tx = qjflList.get(options1).getName();
                        qjflTv.setText(tx);
                    }
                }).build();
                pvOptions.setPicker(qjflList, null, null);
                pvOptions.show();
                break;
            case R.id.qjlx_ll:
                qjlxList.clear();
                if (leave_type == null || leave_type.size() == 0) {
                    return;
                }
                for (int i = 0; i < leave_type.size(); i++) {
                    qjlxList.add(new MoniterBean(leave_type.get(i).getType(), leave_type.get(i).getTitle()));
                }
                OptionsPickerView pvOptions1 = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        String tx = qjlxList.get(options1).getName();
                        Log.d("QjShenqingActivity11", "qjlxList:" + qjlxList);
                        qjlxid = qjlxList.get(options1).getId();
                        qjlxTv.setText(tx);
                        if (tx.equals("公出")) {
                            gclxLl.setVisibility(View.VISIBLE);
                        } else {
                            gclxLl.setVisibility(View.GONE);
                            hdwkhxxLl.setVisibility(View.GONE);
                            hdwhbdjgzLl.setVisibility(View.GONE);
                            wcpxhkhLl.setVisibility(View.GONE);
                            sstshjLl.setVisibility(View.GONE);
                            gclxTv.setText("请选择");
                        }
                    }
                }).build();
                pvOptions1.setPicker(qjlxList, null, null);
                pvOptions1.show();
                break;
            case R.id.gclx_ll:
                gclxList.clear();
                if (gc_list == null || gc_list.size() == 0) {
                    return;
                }
                for (int i = 0; i < gc_list.size(); i++) {
                    gclxList.add(new MoniterBean(gc_list.get(i).getType(), gc_list.get(i).getTitle()));
                }
                OptionsPickerView pvOptions2 = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        String tx = gclxList.get(options1).getName();
                        gclxTv.setText(tx);
                        gclxId = gclxList.get(options1).getId();
                        if (tx.equals("回单位开会、学习")) {
                            hdwkhxxLl.setVisibility(View.VISIBLE);
                            hdwhbdjgzLl.setVisibility(View.GONE);
                            wcpxhkhLl.setVisibility(View.GONE);
                            sstshjLl.setVisibility(View.GONE);
                        } else if (tx.equals("回单位汇报、对接工作")) {
                            hdwhbdjgzLl.setVisibility(View.VISIBLE);
                            hdwkhxxLl.setVisibility(View.GONE);
                            wcpxhkhLl.setVisibility(View.GONE);
                            sstshjLl.setVisibility(View.GONE);
                        } else if (tx.equals("外出培训或开会")) {
                            wcpxhkhLl.setVisibility(View.VISIBLE);
                            hdwkhxxLl.setVisibility(View.GONE);
                            hdwhbdjgzLl.setVisibility(View.GONE);
                            sstshjLl.setVisibility(View.GONE);
                        } else if (tx.equals("实施特殊环节")) {
                            sstshjLl.setVisibility(View.VISIBLE);
                            hdwkhxxLl.setVisibility(View.GONE);
                            hdwhbdjgzLl.setVisibility(View.GONE);
                            wcpxhkhLl.setVisibility(View.GONE);
                        } else {
                            hdwkhxxLl.setVisibility(View.GONE);
                            hdwhbdjgzLl.setVisibility(View.GONE);
                            wcpxhkhLl.setVisibility(View.GONE);
                            sstshjLl.setVisibility(View.GONE);
                        }
                    }
                }).build();
                pvOptions2.setPicker(gclxList, null, null);
                pvOptions2.show();
                break;
            case R.id.kssj_ll:
                TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {

                        kstime = date.getTime() / 1000;
                        kstimestr = kstime + "";

                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        kssjTv.setText(format.format(date));

                        if (!TextUtils.isEmpty(kstimestr)
                                && !TextUtils.isEmpty(jstimestr)) {
                            //请求天数
                            if (!TextUtils.isEmpty(jstimestr)) {
                                //如果已经选择了结束时间 需要判断 开始时间是否小于结束时间
                                if (kstime > jstime) {
                                    kstimestr = "";
                                    Toast.makeText(QjShenqingActivity.this, "开始时间不能大于结束时间", Toast.LENGTH_SHORT).show();
                                    kssjTv.setText("请选择");
                                    return;
                                }

                            }
                            //w网络请求
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("start", kstimestr);
                            jsonObject.put("end", jstimestr);
                            NetRequsetUtil.getInstance().netRequestPostJson("v1/center/get_time_day",
                                    jsonObject.toString(),
                                    new NetDownResponse() {
                                        @Override
                                        public void success(String str) {
                                            Day day = JSON.parseObject(str, Day.class);
                                            qjnumTv.setText(day.getDay());
                                        }

                                        @Override
                                        public void errowithresponse(String str) {

                                        }

                                        @Override
                                        public void erro() {

                                        }

                                        @Override
                                        public void finish() {

                                        }
                                    });
                        }
                    }
                }).setType(new boolean[]{true, true, true, true, true, false})
                        .setTitleText("选择开始时间").build();
                pvTime.show();
                break;
            case R.id.jssj_ll:
                TimePickerView pvTime1 = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {

                        jstime = date.getTime() / 1000;
                        jstimestr = jstime + "";


                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        jssjTv.setText(format.format(date));

                        if (!TextUtils.isEmpty(kstimestr)
                                && !TextUtils.isEmpty(jstimestr)) {
                            //请求天数
                            if (!TextUtils.isEmpty(kstimestr)) {
                                //如果已经选择了开始时间 需要判断 结束时间是否小于开始时间
                                if (kstime > jstime) {
                                    jstimestr = "";
                                    jssjTv.setText("请选择");
                                    Toast.makeText(QjShenqingActivity.this, "结束时间不能小于结束时间", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                //w网络请求
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("start", kstimestr);
                                jsonObject.put("end", jstimestr);
                                NetRequsetUtil.getInstance().netRequestPostJson("v1/center/get_time_day",
                                        jsonObject.toString(),
                                        new NetDownResponse() {
                                            @Override
                                            public void success(String str) {
                                                Day day = JSON.parseObject(str, Day.class);
                                                qjnumTv.setText(day.getDay());
                                            }

                                            @Override
                                            public void errowithresponse(String str) {

                                            }

                                            @Override
                                            public void erro() {

                                            }

                                            @Override
                                            public void finish() {

                                            }
                                        });
                            }
                        }
                    }
                }).setType(new boolean[]{true, true, true, true, true, false})
                        .setTitleText("选择结束时间").build();
                pvTime1.show();
                break;
            case R.id.back_iv:
                finish();
                break;
            case R.id.commit_tv:
                // TODO: 2019/4/29 提交
//
                if (!TextUtils.isEmpty(zzclStr)) {
                    //先上传佐证材料
                    File file = new File(zzclStr);
                    NetRequsetUtil.getInstance().uploadFile("v1/center/do_upload", zzclStr, "file_name", file.getName(), new NetUpLoadResponse() {
                        @Override
                        public void success(String str) {
                            UpSuccessBean upSuccessBean = JSON.parseObject(str, UpSuccessBean.class);
                            String file_url = upSuccessBean.getFile_url();
                            final JSONObject jsonObject = new JSONObject();
                            jsonObject.put("url", file_url);

                            if (!TextUtils.isEmpty(et.getText().toString())){
                                jsonObject.put("content", et.getText().toString());
                            }
                            if (qjflTv.getText().toString().equals("请选择")) {
                                Toast.makeText(QjShenqingActivity.this, "请选择请假分类", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            jsonObject.put("fen_lei",qjflId);
                            if (TextUtils.isEmpty(kstimestr)) {
                                Toast.makeText(QjShenqingActivity.this, "请选择开始时间", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            jsonObject.put("start", kstimestr);
                            if (TextUtils.isEmpty(jstimestr)) {
                                Toast.makeText(QjShenqingActivity.this, "请选择结束时间", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            jsonObject.put("end", jstimestr);

                            jsonObject.put("day", qjnumTv.getText().toString());

                            if (TextUtils.isEmpty(qjlxid)) {
                                Toast.makeText(QjShenqingActivity.this, "请选择请假类型", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            jsonObject.put("type", qjlxid);

                            if (qjlxid.equals("9")) {
                                //公出
                                if (TextUtils.isEmpty(gclxId)) {
                                    Toast.makeText(QjShenqingActivity.this, "请选择公出类型", Toast.LENGTH_SHORT).show();
                                    return;
                                }


                                if (SpUtil.getString
                                        (QjShenqingActivity.this,
                                                SpUtil.Usertype, "").equals("1")) {
                                    //中心代表可以直接上传
                                    jsonObject.put("lg_lx", gclxId);
                                } else {
                                    //其他的做判断
                                    jsonObject.put("lg_lx", gclxId);

                                    if (gclxId.equals("1")) {
                                        jsonObject.put("LHYTZR", hytzrStr);
                                        jsonObject.put("LHYDD", hyddStr);
                                        jsonObject.put("LHYTITORCON", bthnrStr);
                                    }

                                    if (gclxId.equals("2")) {
                                        jsonObject.put("LHBLDNAME", djhbldhdjgzcsrStr);
                                    }

                                    if (gclxId.equals("4")) {
                                        jsonObject.put("LXXORG", pxhyzzdwStr);
                                        jsonObject.put("LXXADD", ddStr);
                                        jsonObject.put("LXXCON", pxhynrStr);


                                        //附件名称
                                        File file1 = new File(pxhytzStr);
                                        if (TextUtils.isEmpty(pxhytzStr)) {
                                            Toast.makeText(QjShenqingActivity.this, "请选择培训会议通知", Toast.LENGTH_SHORT).show();
                                            return;
                                        }
                                        jsonObject.put("LXXTZ", file1.getName());


                                        NetRequsetUtil.getInstance().uploadFile("v1/center/do_upload", pxhytzStr, "file_name", file1.getName(), new NetUpLoadResponse() {
                                            @Override
                                            public void success(String str) {
                                                try {
                                                    UpSuccessBean upSuccessBean = JSON.parseObject(str, UpSuccessBean.class);
                                                    String file_url = upSuccessBean.getFile_url();
                                                    //附件地址
                                                    jsonObject.put("LXXTZURL", file_url);
                                                    jsonObject.put("org_code" , SpUtil.getString(QjShenqingActivity.this,SpUtil.
                                                            ORG_CODE,""));
                                                    if (SpUtil.getString(QjShenqingActivity.this,
                                                            SpUtil.Usertype,""
                                                    ).equals("1")){
                                                        jsonObject.put("MyShid",SpUtil.getString(QjShenqingActivity.this,
                                                                SpUtil.SJLD,""));
                                                        jsonObject.put("MyShN",SpUtil.getString(QjShenqingActivity.this,
                                                                SpUtil.SJLDN,""));
                                                    }
                                                    NetRequsetUtil.getInstance().netRequestPostJson("v1/center/leave_add_function", jsonObject.toString(), new NetDownResponse() {
                                                        @Override
                                                        public void success(String str) {
                                                            Toast.makeText(QjShenqingActivity.this, "申请成功", Toast.LENGTH_SHORT).show();
                                                            Intent intent1 = new Intent(QjShenqingActivity.this,
                                                                    WdqjActivity.class);
                                                            startActivity(intent1);
                                                            QjShenqingActivity.this.finish();

                                                        }

                                                        @Override
                                                        public void errowithresponse(String str) {
                                                            ErrorBeanUtil.getErrorMessage(str);
                                                        }

                                                        @Override
                                                        public void erro() {

                                                        }

                                                        @Override
                                                        public void finish() {

                                                        }
                                                    });
                                                } catch (Exception e) {
                                                    Toast.makeText(QjShenqingActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
                                                }

                                            }

                                            @Override
                                            public void erro() {
                                                Toast.makeText(QjShenqingActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
                                            }
                                        });

                                        return;//上传文件 不用往下走了
                                    }

                                    if (gclxId.equals("6")) {
                                        jsonObject.put("LTSSQSXNAME", sqsxmcStr);
                                        jsonObject.put("LTSHJMC", tshjmcStr);
                                        jsonObject.put("LTSADD", sstshjDzStr);
                                    }
                                }


                            }
                            jsonObject.put("org_code" , SpUtil.getString(QjShenqingActivity.this,SpUtil.
                                    ORG_CODE,""));
                            if (SpUtil.getString(QjShenqingActivity.this,
                                    SpUtil.Usertype,""
                            ).equals("1")){
                                jsonObject.put("MyShid",SpUtil.getString(QjShenqingActivity.this,
                                        SpUtil.SJLD,""));
                                jsonObject.put("MyShN",SpUtil.getString(QjShenqingActivity.this,
                                        SpUtil.SJLDN,""));
                            }
                            Log.d("aa", jsonObject.toString());
                            NetRequsetUtil.getInstance().netRequestPostJson("v1/center/leave_add_function", jsonObject.toString(), new NetDownResponse() {
                                @Override
                                public void success(String str) {
                                    Toast.makeText(QjShenqingActivity.this, "申请成功", Toast.LENGTH_SHORT).show();
                                    QjShenqingActivity.this.finish();
                                    Intent intent1 = new Intent(QjShenqingActivity.this,
                                            WdqjActivity.class);
                                    startActivity(intent1);
                                }

                                @Override
                                public void errowithresponse(String str) {
                                    ErrorBeanUtil.getErrorMessage(str);
                                }

                                @Override
                                public void erro() {

                                }

                                @Override
                                public void finish() {

                                }
                            });
                        }

                        @Override
                        public void erro() {
                            Log.d("QjShenqingActivitysss", "erro");
                        }
                    });
                } else {
                    //先佐证材料为空
                    final JSONObject jsonObject = new JSONObject();
                    if (!TextUtils.isEmpty(et.getText().toString())){
                        jsonObject.put("content", et.getText().toString());
                    }
                    if (qjflTv.getText().toString().equals("请选择")) {
                        Toast.makeText(QjShenqingActivity.this, "请选择请假分类", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    jsonObject.put("fen_lei",qjflId);
                    if (TextUtils.isEmpty(kstimestr)) {
                        Toast.makeText(QjShenqingActivity.this, "请选择开始时间", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    jsonObject.put("start", kstimestr);
                    if (TextUtils.isEmpty(jstimestr)) {
                        Toast.makeText(QjShenqingActivity.this, "请选择结束时间", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    jsonObject.put("end", jstimestr);

                    jsonObject.put("day", qjnumTv.getText().toString());

                    if (TextUtils.isEmpty(qjlxid)) {
                        Toast.makeText(QjShenqingActivity.this, "请选择请假类型", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    jsonObject.put("type", qjlxid);

                    if (qjlxid.equals("9")) {
                        //公出
                        if (TextUtils.isEmpty(gclxId)) {
                            Toast.makeText(QjShenqingActivity.this, "请选择公出类型", Toast.LENGTH_SHORT).show();
                            return;
                        }


                        if (SpUtil.getString
                                (QjShenqingActivity.this,
                                        SpUtil.Usertype, "").equals("1")) {
                            //中心代表可以直接上传
                            jsonObject.put("lg_lx", gclxId);
                        } else {
                            //其他的做判断
                            jsonObject.put("lg_lx", gclxId);

                            if (gclxId.equals("1")) {
                                jsonObject.put("LHYTZR", hytzrStr);
                                jsonObject.put("LHYDD", hyddStr);
                                jsonObject.put("LHYTITORCON", bthnrStr);
                            }

                            if (gclxId.equals("2")) {
                                jsonObject.put("LHBLDNAME", djhbldhdjgzcsrStr);
                            }

                            if (gclxId.equals("4")) {
                                jsonObject.put("LXXORG", pxhyzzdwStr);
                                jsonObject.put("LXXADD", ddStr);
                                jsonObject.put("LXXCON", pxhynrStr);


                                //附件名称
                                File file1 = new File(pxhytzStr);
                                if (TextUtils.isEmpty(pxhytzStr)) {
                                    Toast.makeText(QjShenqingActivity.this, "请选择培训会议通知", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                jsonObject.put("LXXTZ", file1.getName());

                                NetRequsetUtil.getInstance().uploadFile("v1/center/do_upload", pxhytzStr, "file_name", file1.getName(), new NetUpLoadResponse() {
                                    @Override
                                    public void success(String str) {
                                        try {
                                            UpSuccessBean upSuccessBean = JSON.parseObject(str, UpSuccessBean.class);
                                            String file_url = upSuccessBean.getFile_url();
                                            //附件地址
                                            jsonObject.put("LXXTZURL", file_url);
                                            jsonObject.put("org_code" , SpUtil.getString(QjShenqingActivity.this,SpUtil.
                                                    ORG_CODE,""));
                                            if (SpUtil.getString(QjShenqingActivity.this,
                                                    SpUtil.Usertype,""
                                            ).equals("1")){
                                                jsonObject.put("MyShid",SpUtil.getString(QjShenqingActivity.this,
                                                        SpUtil.SJLD,""));
                                                jsonObject.put("MyShN",SpUtil.getString(QjShenqingActivity.this,
                                                        SpUtil.SJLDN,""));
                                            }
                                            NetRequsetUtil.getInstance().netRequestPostJson("v1/center/leave_add_function", jsonObject.toString(), new NetDownResponse() {
                                                @Override
                                                public void success(String str) {
                                                    Toast.makeText(QjShenqingActivity.this, "申请成功", Toast.LENGTH_SHORT).show();
                                                    QjShenqingActivity.this.finish();
                                                    Intent intent1 = new Intent(QjShenqingActivity.this,
                                                            WdqjActivity.class);
                                                    startActivity(intent1);
                                                }

                                                @Override
                                                public void errowithresponse(String str) {
                                                    ErrorBeanUtil.getErrorMessage(str);
                                                }

                                                @Override
                                                public void erro() {

                                                }

                                                @Override
                                                public void finish() {

                                                }
                                            });
                                        } catch (Exception e) {
                                            Toast.makeText(QjShenqingActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
                                        }

                                    }

                                    @Override
                                    public void erro() {
                                        Toast.makeText(QjShenqingActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
                                    }
                                });

                                return;//上传文件 不用往下走了
                            }

                            if (gclxId.equals("6")) {
                                jsonObject.put("LTSSQSXNAME", sqsxmcStr);
                                jsonObject.put("LTSHJMC", tshjmcStr);
                                jsonObject.put("LTSADD", sstshjDzStr);
                            }
                        }


                    }
                    jsonObject.put("org_code" , SpUtil.getString(QjShenqingActivity.this,SpUtil.
                            ORG_CODE,""));
                    if (SpUtil.getString(QjShenqingActivity.this,
                            SpUtil.Usertype,""
                    ).equals("1")){
                        jsonObject.put("MyShid",SpUtil.getString(QjShenqingActivity.this,
                                SpUtil.SJLD,""));
                        jsonObject.put("MyShN",SpUtil.getString(QjShenqingActivity.this,
                                SpUtil.SJLDN,""));
                    }
                    Log.d("aa", jsonObject.toString());
                    NetRequsetUtil.getInstance().
                            netRequestPostJson("v1/center/leave_add_function",
                                    jsonObject.toString(), new NetDownResponse() {
                        @Override
                        public void success(String str) {
                            Toast.makeText(QjShenqingActivity.this, "申请成功", Toast.LENGTH_SHORT).show();
                            QjShenqingActivity.this.finish();
                            Intent intent1 = new Intent(QjShenqingActivity.this,
                                    WdqjActivity.class);
                            startActivity(intent1);
                        }

                        @Override
                        public void errowithresponse(String str) {
                            ErrorBeanUtil.getErrorMessage(str);
                        }

                        @Override
                        public void erro() {

                        }

                        @Override
                        public void finish() {

                        }
                    });
                }
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 0:
                if (data != null) {
                    photos = (ArrayList<String>) data.getExtras().getSerializable("photos");//path是选择拍照或者图片的地址数组
                    if (photos != null) {
                        localUrl.addAll(photos);
                    }
                    adapter.setImages(localUrl);

                }
                break;
            case 5:
                if (data != null) {
                    hytzrStr = data.getStringExtra(ContantActivity.returnkey);
                    hytzrTv.setText(hytzrStr);
                }
                break;
            case 6:
                if (data != null) {
                    hyddStr = data.getStringExtra(ContantActivity.returnkey);
                    hyddTv.setText(hyddStr);
                }
                break;
            case 7:
                if (data != null) {
                    bthnrStr = data.getStringExtra(ContantActivity.returnkey);
                    bthnrTv.setText(bthnrStr);
                }
                break;
            case 8:
                if (data != null) {
                    djhbldhdjgzcsrStr = data.getStringExtra(ContantActivity.returnkey);
                    djhbldhdjgzcsTv.setText(djhbldhdjgzcsrStr);
                }
                break;
            case 9:
                if (data != null) {
                    pxhyzzdwStr = data.getStringExtra(ContantActivity.returnkey);
                    pxhyzzdwTv.setText(pxhyzzdwStr);
                }
                break;
            case 10:
                if (data != null) {
                    pxhynrStr = data.getStringExtra(ContantActivity.returnkey);
                    pxhynrTv.setText(pxhynrStr);
                }
                break;
            case 11:
                if (data != null) {
                    sqsxmcStr = data.getStringExtra(ContantActivity.returnkey);
                    sqsxmcTv.setText(sqsxmcStr);
                }
                break;
            case 12:
                if (data != null) {
                    tshjmcStr = data.getStringExtra(ContantActivity.returnkey);
                    tshjmcTv.setText(tshjmcStr);
                }
                break;
            case 13:
                if (data != null) {
                    sstshjDzStr = data.getStringExtra(ContantActivity.returnkey);
                    sstshjdzTv.setText(sstshjDzStr);
                }
                break;
            case 14:
                if (data != null) {
                    ddStr = data.getStringExtra(ContantActivity.returnkey);
                    ddTv.setText(ddStr);
                }
                break;
            case 99:
                if (resultCode == RESULT_OK) {
                    Uri data1 = data.getData();
                    if (data1 == null) {
                        return;
                    }
                    String path = getPath(QjShenqingActivity.this, data1);
                    Log.d("QjShenqingActivitysss", path);
                    File file = new File(path);
                    if (file.exists()) {
                        wjmPxhytzTv.setText(file.getName());
                        pxhytzStr = path;
                    }
                }
                break;
            case 100:
                if (resultCode == RESULT_OK) {
                    Uri data1 = data.getData();
                    if (data1 == null) {
                        return;
                    }
                    String path = getPath(QjShenqingActivity.this, data1);
                    zzclStr = path;
                    File file = new File(path);
                    if (file.exists()) {
                        wjm_tv.setText(file.getName());
                    }
                }
                break;
            default:
                break;
        }
    }

    public String getPath(final Context context, final Uri uri) {

        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
//                Log.i(TAG,"isExternalStorageDocument***"+uri.toString());
//                Log.i(TAG,"docId***"+docId);
//                以下是打印示例：
//                isExternalStorageDocument***content://com.android.externalstorage.documents/document/primary%3ATset%2FROC2018421103253.wav
//                docId***primary:Test/ROC2018421103253.wav
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {
//                Log.i(TAG,"isDownloadsDocument***"+uri.toString());
                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                return getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
//                Log.i(TAG,"isMediaDocument***"+uri.toString());
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[]{split[1]};

                return getDataColumn(context, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
//            Log.i(TAG,"content***"+uri.toString());
            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
//            Log.i(TAG,"file***"+uri.toString());
            return uri.getPath();
        }
        return null;
    }

    /**
     * Get the value of the data column for this Uri. This is useful for
     * MediaStore Uris, and other file-based ContentProviders.
     *
     * @param context       The context.
     * @param uri           The Uri to query.
     * @param selection     (Optional) Filter used in the query.
     * @param selectionArgs (Optional) Selection arguments used in the query.
     * @return The value of the _data column, which is typically a file path.
     */
    public String getDataColumn(Context context, Uri uri, String selection,
                                String[] selectionArgs) {

        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {column};

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(column_index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }


    public boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    public boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    public boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
